var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';

import { $wuxRefresher } from '../../components/wux';
import { $wuxPrompt } from '../../components/wux';

var app = getApp();
var that;

const sliderWidth = 96
let  pageInex = 1;

Page({
  data: { 
    tabIndex:'0',
    user:null,
    activeIndex: '0',
    sliderOffset: 0,
    sliderLeft: 0,
    list : [],
    isLoadMore : false,
    isEnd : false,
    isEmpty:false,

    htopen:false,
    propen:false,
    navs:false,
    hotDefault:'所有数据',
    hot_choseIndex : 0,
    isfull:false,
    isAll:0,
    chose:0

  },
 
  onLoad: function (query) {
    that = this;
    wx.setNavigationBarTitle( { title: "D几名竞赛授权码" });
    $wuxPrompt.init('msg1', {
      title: '暂时没有相关数据', 
    }).show()

    this.query = query;

    that.setData({
      ht : ["所有数据","已使用的","未使用的"]
    });

    that.refresher = new $wuxRefresher({
      onPulling() {
      },
      onRefresh() {
        pageInex = 1;
        this.start=true;
        that.setData({
          list : [],
          isLoadMore : false,
          isEnd : false,
          isEmpty:false
        });

        setTimeout(() => {
          that.loadData();
        },1000);
      }
    });
  }
  ,
  hot_action: function(e) {
      var s;
      if(that.data.htopen) {
          s = false;
      } else {
          s = true;
      }
      that.setData({
          chose: 1,
          navs:s,
          isfull:s,
          htopen:!that.data.htopen,
          propen:false
      });
  },
    //热门
  hot_type:function (e) {
      var dataset = e.currentTarget.dataset;
      var s;
      if(that.data.htopen) {
          s = false;
      } else {
          s = true;
      }
      that.setData({
          hot_choseIndex:dataset.choseindex,
          htopen:!that.data.htopen,
          navs:s,
          isfull:s,
          hotDefault:that.data.ht[dataset.choseindex]
      });
      that.requestData();
  },
  hidebg : function () {
    that.setData({
        isfull:false,
        htopen:false,
        propen:false,
        navs:false
    });
  },
  touchstart(e) {
    that.refresher.touchstart(e)
  }, 
  touchmove(e) {
    that.refresher.touchmove(e)
  }, 
  touchend(e) {
    that.refresher.touchend(e)
  }, 
  requestData: function() {
      // util.alert(that.data.hot_choseIndex);
      util.showLoading();

      pageInex = 1;
      that.setData({
        list : [],
        isLoadMore : false,
        isEnd : false,
        isEmpty:false
      });
        
      that.loadData();
  },
  
  changeTab:function(e) {
      console.log(e)
      var index = e.currentTarget.dataset.tabIndex;
      switch (index){
          case '0':
              that.setData({
                  tabIndex:index,
              });
              break;
          case '1':
              that.setData({
                  tabIndex:index,
              });
              break;
      }
  },

  onReady : function(){
    pageInex = 1;
    util.showLoading();
    app.getUserInfo()
    .then(res => {
      that.setData({
        user:res
      })
      that.loadData(); 
    });

  },
  // onReachBottom
  onReachBottom : function(e,a){
      //加载更多
      if(!that.data.isEnd && !that.data.isLoadMore && !that.data.isEmpty){
          that.loadData();
      }
  },
  
  onForwardColumn : function(){
      wx.navigateTo({
        url : "../column/column"
      });
  },

  onPreImage : function(e){
      let src = e.currentTarget.dataset.imgSrc;
      Util.UI.preImage(src);
  },
  loadData: function(){

    that.setData({
      isLoadMore : true
    });

    var code= "";
    if(that.data.hot_choseIndex ==1){
      code ="kb";
    }

    var url = config.url_sqm_list;
    var params = {
      // uid:111111111,
      pageIndex: pageInex,
      pageSize: app.globalData.defaultPageSize,
      code:code
    }

    wxRequest.postRequest(url, params)
    .then(res=>{
      
      if(that.refresher.start==true){
        that.refresher.start=false;
        that.refresher.events.emit(`scroll.refreshComplete`);
      }

      if(pageInex==1 && res.length==0){
          that.setData({
            isLoadMore : false,
            isEnd : false,
            isEmpty :true,
          });
          return;
      }
      
      if(res.length > 0){
        pageInex++;
        var data = that.data.list.concat(res);   
        that.setData({
            list : data,
            isLoadMore : false
        });
      }
      
      if(res.length < app.globalData.defaultPageSize || res.length == 0 ){
        that.setData({
          isLoadMore : false,
          isEnd : true
        });
      }

    })
    .catch(res => {
      util.alert(res.message);
    })
    .finally(function (res) {
      util.hideLoading();
    });
  },
  btnadd:function(){

    wx.navigateTo({
      url: '../user/sqm_edit?id=0'
    });

  },
  rowedit:function(e){

    var data = e.currentTarget.dataset;
    var id = data.id;
    wx.navigateTo({
      url: '../user/sqm_edit?id='+id
    });

  },
  rowdel:function(e){
    
    var data = e.currentTarget.dataset;
    var id = data.id;

    util.showLoading();
    var url = config.url_sqm_del;
    var params = {
      id:id     
    }
    wxRequest.postRequest(url, params)
    .then(res=>{
      if(res==21){
        util.alert("授权码已经使用,禁止删除!");
        return;
      }
      else if(res ==1){
        util.alert("删除成功!",function(){
          that.requestData();
        });

      }

    })
    .catch(res => {
      util.alert(res.message);
    })
    .finally(function (res) {
      util.hideLoading();
    });
  }

  
})


